package helpers;

import play.api.mvc.Call;
import play.mvc.Http.Context;
import play.mvc.Http.Flash;

public class ClientRequest {
	public int appId;
	public int marketKey;
	
	public int deviceKey;
	
	public String module;
	
	public ClientRequest(int appId, int marketKey, int deviceKey){
		this.appId = appId;
		this.marketKey = marketKey;
		this.deviceKey = deviceKey;
	}
	
	public static void setCurrent()
	{
		Context ctx = Context.current();
		
		String appId = "0";
	    String marketKey = "0";
	    String deviceKey = "0";
	    
	    if(ctx.request().getQueryString("app") != null)
	    	appId = ctx.request().getQueryString("app");
	    
	    if(ctx.request().getQueryString("market") != null)
    		marketKey = ctx.request().getQueryString("market");
	    
	    if(ctx.request().getQueryString("device") != null)
	    	deviceKey 	= ctx.request().getQueryString("device");
	    
	    Flash flash = ctx.flash();
	    flash.put("appId", appId);
	    flash.put("marketKey", marketKey);
	    flash.put("deviceKey", deviceKey);
	}
	
	public static ClientRequest getCurrent()
	{
		Context ctx = Context.current();
	    Flash flash = ctx.flash();
	    
	    int appId 	= Integer.parseInt(flash.get("appId"));
	    int marketKey = Integer.parseInt(flash.get("marketKey"));
	    int deviceKey 	= Integer.parseInt(flash.get("deviceKey"));
	    
	    ClientRequest clientRequest = new ClientRequest(appId, marketKey, deviceKey);
	    
	    return clientRequest;
	}
	
	public void showDetail()
	{
		System.out.println("appId: " + this.appId + " - marketKey: " + this.marketKey + " - deviceKey: " + this.deviceKey);
	}
	
	public static String getUrlWithParams(Call call)
	{
		String url = call.url();
		
		Context ctx = Context.current();
		
		String uri = ctx.request().uri();
		
		String[] arrString = uri.split("\\?");
		
		if(arrString.length > 1)
			url += "?" + arrString[1];
		
		return url;
	}
}
